﻿#region copyright
//<copyright>
// Copyright(C) 2012 TrackerRealm Corporation
// This file is part of the open source project - Jazz. http://jazz.codeplex.com
// 
// Jazz is open software: you can redistribute it and/or modify it 
// under the terms of the GNU Affero General Public License (AGPL) as published by 
// the Free Software Foundation, version 3 of the License.
// 
// Jazz is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty 
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
//  See the GNU Affero General Public License (AGPL) for more details.
// 
// You should have received a copy of the GNU General Public 
// License along with Jazz.  If not, see <http://www.gnu.org/licenses/>.
//
// REMOVAL OF THIS NOTICE IS VIOLATION OF THE COPYRIGHT. 
//</copyright>
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using TrackerRealm.Jazz.Common;

namespace TrackerRealm.Jazz.Client
{
    /// <summary>
    /// Wraps a 'MethodInfo' object representing a proptery read accessor (getter).
    /// </summary>
    public class jReadAccessor: jMethodInfo
    {
        /// <summary>
        /// 
        /// </summary>
        public static jReadAccessor Empty = new jReadAccessorEmpty();
        /// <summary>
        /// 
        /// </summary>
        /// <param name="method"></param>
        /// <param name="prop"></param>
        /// <param name="jObj"></param>
        internal jReadAccessor(MethodInfo method, jPropertyInfo prop, jObject jObj)
        :base(method, jObj)
        {
            this.Property = prop;
            if (prop != null) // will be null when empty
            {
                if (prop.IsUI) this.isUI = true;
            }
            //if (prop != null)  // will be null when empty
            //{
            //    prop.CanRead = true;
            //    if (prop.IsUI) this.isUI = true;
            //    if (jObj != null)
            //    {
            //        prop.CanRead = method.IsGrantedAccess(jObj);
            //    }
            //}
        }
        /// <summary>
        /// Returns the Property that contains this 'Read' accessor.
        /// </summary>
        public jPropertyInfo Property
        {   get;
            private set;
        }

        #region CanRead
        /// <summary>
        /// Returns true if 'IsProperty' is true and if the 'Read' accessor is usable
        /// </summary>
        public override bool CanRead
        {
            get
            {
                return this.Property.CanRead;
            }
        }
        #endregion
        /// <summary>
        /// 
        /// </summary>
        /// <param name="wf"></param>
        /// <returns></returns>
        public virtual object Get(jObject wf)
        {
            return this.MethodInfo.Invoke(wf, new object[0]);
        }
        #region IsReadAccessor
        /// <summary>
        /// Returns true if this member is a property getter.
        /// <para>Note: Property setter and getters are methods.</para>
        /// </summary>
        public override bool IsReadAccessor
        {
            get { return true; }
        }
        #endregion
        #region Empty jReadAccessorEmpty Class
        /// <summary>
        /// 
        /// </summary>
        private class jReadAccessorEmpty : jReadAccessor
        {
            /// <summary>
            /// 
            /// </summary>
            internal jReadAccessorEmpty()
                : base(null, null, null)
            {
            }
            /// <summary>
            /// 
            /// </summary>
            public override bool IsEmpty
            {
                get
                {
                    return true;
                }
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="wf"></param>
            /// <returns></returns>
            public override object Get(jObject wf)
            {
                throw new AccessViolationException();
            }
            #region CanRead
            /// <summary>
            /// Returns true if 'IsProperty' is true and if the 'Read' accessor is usable
            /// </summary>
            public override bool CanRead
            {
                get{ return false; }
            }
            #endregion
        }
        #endregion
    }
}
